package com.th.supcom.pds.impl.service.internal;

import java.util.ArrayList;
import java.util.List;

import net.carefx.fc.framework.web.gwt.util.FcUtils;
import net.carefx.framework.exception.BusinessException;
import net.carefx.framework.translate.TranslatorManager;
import net.carefx.framework.util.ConvertUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.th.supcom.pds.domain.pojo.PdsPresc;
import com.th.supcom.pds.domain.vo.PatientInfo;
import com.th.supcom.pds.domain.vo.PdsPrescDetailAndPrescVo;
import com.th.supcom.pds.domain.vo.PrescAndPatiResult;
import com.th.supcom.pds.impl.common.enums.ChargeIndicator;
import com.th.supcom.pds.impl.manager.PdsPrescDetailManager;
import com.th.supcom.pds.impl.manager.PdsPrescManager;
import com.th.supcom.pds.internal.api.RefundApplicationService;
import com.th.supcom.pds.medicare.api.AdapterPtsExtOutPatientVisitService;

/**
 * @author yzhang
 */
public class RefundApplicationServiceImpl implements RefundApplicationService
{
    private PdsPrescDetailManager m_pdsPrescDetailManager;
    private PdsPrescManager m_pdsPrescManager;
    private TranslatorManager m_translatorManager;
    private AdapterPtsExtOutPatientVisitService m_adapterPtsExtOutPatientVisitService;

    /**
     * 用于门诊退费处方状态查询
     */
    @Override
    public PrescAndPatiResult getPrescDetailForRefund (List <String> prescNoList, String performedBy)
    {
        s_logger.debug ("进入getPrescDetailForRefund方法,入参:prescNoList,o{},performedBy:{}", new Object[]
        { prescNoList, performedBy });
        PrescAndPatiResult prescAndPatiResult = new PrescAndPatiResult ();

        List <List <PdsPrescDetailAndPrescVo>> prescDetailAndPrescList = new ArrayList <List <PdsPrescDetailAndPrescVo>> ();
        try
        {
            for (String prescNo : prescNoList)
            {
                PdsPrescDetailAndPrescVo vo = new PdsPrescDetailAndPrescVo ();
                vo.setPrescNo (prescNo);
                vo.setPperformedBy (performedBy);
                List <PdsPrescDetailAndPrescVo> list = m_pdsPrescDetailManager.getPrescDetailForRefund (vo);
                if (list.isEmpty ())
                {
                    continue;
                }
                list = m_translatorManager.translate (list);
                prescDetailAndPrescList.add (list);

            }
        }
        catch (Exception e)
        {
            s_logger.error ("查询处方信息出现异常" + e.getLocalizedMessage ());
            throw new BusinessException ("查询处方信息出现异常，请联系计算机中心！", e);
        }
        if (prescDetailAndPrescList.isEmpty ())
        {
            throw new BusinessException ("找不到相关处方信息！");
        }
        if (1 == prescNoList.size ())
        {
            // 得到患者信息
            Long pVisitId = prescDetailAndPrescList.get (0).get (0).getPatientVisitId ();
            String patientId = prescDetailAndPrescList.get (0).get (0).getPatientId ();
            if (pVisitId == null)
            {
                throw new BusinessException ("查询失败，相关处方找不到患者ID");
            }
            PatientInfo patientInfo = null;
            try
            {
                patientInfo = m_adapterPtsExtOutPatientVisitService.getPatientInfo (pVisitId,
                                                                                    prescDetailAndPrescList.get (0)
                                                                                                           .get (0)
                                                                                                           .getPrescSource (),
                                                                                    patientId);
            }
            catch (Exception e)
            {
                s_logger.error ("查询患者信息出现异常" + e.getLocalizedMessage ());
                throw new BusinessException ("查询患者信息出现异常，请联系计算机中心！", e);
            }
            if (patientInfo == null)
            {
                throw new BusinessException ("查询患者信息出现异常，请联系计算机中心！");
            }
            prescAndPatiResult.setPatientInfo (patientInfo);
        }
        prescAndPatiResult.setList (prescDetailAndPrescList);

        return prescAndPatiResult;

    }

    /**
     * 门诊退费申请
     */
    @Override
    public void modifyrefundApplicate (List <PdsPrescDetailAndPrescVo> prescList,
                                       List <PdsPrescDetailAndPrescVo> prescDetailList, String operator)
    {

        // 3回写pds处方信息,修改计价标志：已申请，退费申请人，退费时间
        List <PdsPresc> pdsPrescs = ConvertUtils.convert (PdsPresc.class, prescList);
        for (PdsPresc pdsPresc : pdsPrescs)
        {
            pdsPresc.setChargeIndicator (ChargeIndicator.Applied.getEnumCode ());
            pdsPresc.setRefundAppl (operator);
            pdsPresc.setRefundApplTime (FcUtils.getCurrentServerDateTimeForServer ());
        }
        m_pdsPrescManager.modifyPdsPrescListForRefund (pdsPrescs);

        // 回写医嘱处方

    }

    public void setTranslatorManager (TranslatorManager translatorManager)
    {
        m_translatorManager = translatorManager;
    }

    public void setPdsPrescDetailManager (PdsPrescDetailManager pdsPrescDetailManager)
    {
        m_pdsPrescDetailManager = pdsPrescDetailManager;
    }

    public void setPdsPrescManager (PdsPrescManager pdsPrescManager)
    {
        m_pdsPrescManager = pdsPrescManager;
    }

    public void setAdapterPtsExtOutPatientVisitService (AdapterPtsExtOutPatientVisitService adapterPtsExtOutPatientVisitService)
    {
        m_adapterPtsExtOutPatientVisitService = adapterPtsExtOutPatientVisitService;
    }

    /** 日志记录器. */
    public static final Logger s_logger = LoggerFactory.getLogger (AuditPrescServiceImpl.class);

    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, XuNuo Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";
}
